#include "lrcpage.h"
#include "ui_lrcpage.h"
#include<QFile>
#include<QDebug>
LrcPage::LrcPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LrcPage)
{
    ui->setupUi(this);
    _animation = new QPropertyAnimation(this,"geometry",this);
}

LrcPage::~LrcPage()
{
    delete ui;
}

bool LrcPage::parseLrc(const QString &path)
{
    //进来之前先清除上一首残留；
    _lrcLines.clear();
    QFile file(path);
    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug()<<"打开文件失败";
        return false;
    }
    //就需要一行一行的读取；
    while(!file.atEnd())
    {
        QString lrcWord = file.readLine(1024);
        //分割成left和right；
        int pos = lrcWord.indexOf(']');
        QString lrcleft = lrcWord.mid(1,pos - 1);
        QString lrcRight = lrcWord.mid(pos + 1);
        lrcRight.resize(lrcRight.size() - 1);//去掉\n;
        //分析时间；
        qint64 time = 0;
        int start = 0;
        pos = lrcleft.indexOf(':');
        start = pos;
        //分
        time += lrcleft.mid(0,pos).toInt()*1000*60;
        //秒
        pos = lrcleft.indexOf('.',pos + 1);
        time += lrcleft.mid(start + 1,pos - start - 1).toInt()*1000;
        //毫秒
        start = pos;
        pos = lrcleft.indexOf('.',pos + 1);
        time += lrcleft.mid(start + 1,pos - start -1).toInt();
        _lrcLines.push_back(LrcLine(lrcRight,time));
    }
    for(auto e:_lrcLines)
    {
        qDebug()<<e._time<<":"<<e._lrcWord;
    }
    return true;
}

void LrcPage::showLrcWord(qint64 position)
{
    //通过当前播放的下标跟新歌词；
    int index = this->getCurrentIndex(position);
    if(index == -1)
    {
        ui->line1->setText("");
        ui->line2->setText("");
        ui->line3->setText("");
        ui->lineCenter->setText("没有歌词");
        ui->line4->setText("");
        ui->line5->setText("");
        ui->line6->setText("");
    }
    else
    {
        ui->line1->setText(getLineWord(index - 3));
        ui->line2->setText(getLineWord(index - 2));
        ui->line3->setText(getLineWord(index - 1));
        ui->lineCenter->setText(getLineWord(index));
        ui->line4->setText(getLineWord(index + 1));
        ui->line5->setText(getLineWord(index + 2));
        ui->line6->setText(getLineWord(index + 3));
    }

}

int LrcPage::getCurrentIndex(qint64 position)
{
    if(_lrcLines.empty())
    {
        //没有歌词；
        return -1;
    }
    if(_lrcLines[0]._time>position)
    {
        //还没有播放；
        return 0;
    }
    for(int i = 1;i<_lrcLines.size();i++)
    {
        //前面一首还没有唱完；
        if(position>_lrcLines[i - 1]._time && position <= _lrcLines[i]._time)
        {
            return i - 1;
        }
    }
    //如果没有找到，可能是在最后结束时；
    return _lrcLines.size() - 1;
}

QString LrcPage::getLineWord(int index)
{
    if(index < 0 || index >= _lrcLines.size())
    {
        return "";
    }
    return _lrcLines[index]._lrcWord;
}

void LrcPage::on_hideBtn_clicked()
{
    //隐藏；
    _animation->setDuration(250);
    _animation->setStartValue(QRect(0,0,width(),height()));
    _animation->setEndValue(QRect(0,width(),width(),height()));
    _animation->start();
}
